package com.xtt.httpclient;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import com.gargoylesoftware.htmlunit.javascript.host.fetch.Response;

public class HttpClient {
	
	CloseableHttpClient client ;
	@Test
	public void test1() throws ClientProtocolException, IOException {
		CloseableHttpClient client = HttpClients.createDefault();//创建HttpClient实例
		HttpGet get = new HttpGet("https://www.taobao.com/");//创建HttpGet实例
		CloseableHttpResponse response = client.execute(get);////执行get请求获取响应
		HttpEntity entity = response.getEntity();//获取响应实体
		String result = EntityUtils.toString(entity, "utf-8");//编码格式根据网页编码更换
		System.out.println(result);
		response.close();//关闭流
		client.close();
	}
	@Test
	public void test2() throws ClientProtocolException, IOException {
		CloseableHttpClient client = HttpClients.createDefault();
		HttpGet get = new HttpGet("http://www.tuicool.com/");//需要模拟浏览器
		
		get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) "
				+ "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");//模拟google
		
		HttpHost host = new HttpHost("119.5.1.13",808);//高匿代理 //使用爬虫抓代理ip
		
		RequestConfig config = RequestConfig.custom()
				.setConnectTimeout(10000)//连接超时10s
				.setSocketTimeout(10000)//读取超时10s
				//.setProxy(host)//代理分为透明代理、匿名代理、混淆代理、高匿代理, 使用高匿代理 (西刺免费代理IP)
				.build();//创建config
		get.setConfig(config);
		
		CloseableHttpResponse response = client.execute(get);
		
		Header[] headers = response.getAllHeaders();//获取所有响应头
		for(int i = 0; i < headers.length; i++) {
			System.out.println(headers[i].getName()+":"+headers[i].getValue());
		}
		
		Header[] headers2 = response.getHeaders("Content-Type");//获取指定响应头
		System.out.println(headers2[0].getName()+"::"+headers2[0].getValue());//Content-Type:text/html; charset=utf-8
		
		StatusLine statusLine = response.getStatusLine();//获取相应状态码，版本
		System.out.println(statusLine);//HTTP/1.1 200 OK
		ProtocolVersion protocalVersion = statusLine.getProtocolVersion();//HTTP/1.1
		int statusCode = statusLine.getStatusCode();//200
		System.out.println(protocalVersion);
		System.out.println(statusCode);
		
		Locale locale = response.getLocale();//获取响应语言
		System.out.println(locale.toString());//zh_CN
		System.out.println(locale.getCountry());//CN
		System.out.println(locale.getLanguage());//zh
		System.out.println(locale.getVariant());//""
		
		ProtocolVersion version = response.getProtocolVersion();
		System.out.println(version);// HTTP/1.1
		System.out.println(version.getProtocol());//HTTP
		System.out.println(version.getMajor());//1
		System.out.println(version.getMinor());//1
		
		HttpEntity entity = response.getEntity();

		
		
		Header contentType = entity.getContentType();//通过HttpEntity获取指定头
		System.out.println(contentType.getName()+":"+contentType.getValue()); //Content-Type:text/html; charset=utf-8
		HeaderElement[] elements = contentType.getElements();
		for(HeaderElement element : elements) {
			System.out.println(element.getName()+":"+element.getValue());//text/html:null
		}
		
		Header contentEncoding = entity.getContentEncoding();//获得响应编码 输出为null
		if(contentEncoding != null) {
			System.out.println(contentEncoding.getName()+":"+contentEncoding.getValue());
		}
		
		String result = EntityUtils.toString(entity, "utf-8");
		//System.out.println(result);
		response.close();
		client.close();
	}
	@Test
	public void test3() throws ParseException, IOException {
		CloseableHttpResponse response = null;
		try {
			HttpHost proxyHost = new HttpHost("119.5.1.13", 808);
			response = HttpClientUtils.getResponse("http://www.xicidaili.com/nn", null, 100000,
					100000);
			HttpEntity entity = response.getEntity();
			BufferedHttpEntity bfEntity = new BufferedHttpEntity(entity);//包装成可以重复使用
			System.out.println(bfEntity.isRepeatable());
			InputStream in = bfEntity.getContent();
//			BufferedInputStream bis = new BufferedInputStream(in);
//			bis.mark(Integer.MAX_VALUE);
			String str = IOUtils.toString(in, "utf-8");
//			bis.reset();
			System.out.println(str);
			response.setEntity(bfEntity);
			String str2 = getResult(response);
//			InputStream in2 = entity.getContent();
//			String str2 = IOUtils.toString(in2, "utf-8");
			System.out.println(str2);
		} finally {
			if(response != null) {
				response.close();
			}
		}
	}
	
	public static String getResult(HttpResponse response) {
		String result = null;
		try {
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				result = EntityUtils.toString(entity,"UTF-8");
			}
			EntityUtils.consume(entity);
		} catch(Exception e) {
			//
		} finally {
		}
		return result;
	}
	/**
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	@Test
	public void testThread() throws ClientProtocolException, IOException {
		ExecutorService service = Executors.newCachedThreadPool();
		for(int i = 0; i < 20; i++) {
			final Integer j = i;
			System.out.println("kaishi");
			service.execute(new Runnable() {
				@Override
				public void run() {
					try {
						System.out.println(j);
						client = HttpClients.createDefault();
						System.out.println(client);
						HttpGet get = new HttpGet("https://www.baidu.com/");//需要模拟浏览器
						
						get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) "
								+ "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");//模拟google
						
						HttpHost host = new HttpHost("119.5.1.13",808);//高匿代理 //使用爬虫抓代理ip
						
						RequestConfig config = RequestConfig.custom()
								.setConnectTimeout(10000)//连接超时10s
								.setSocketTimeout(10000)//读取超时10s
								//.setProxy(host)//代理分为透明代理、匿名代理、混淆代理、高匿代理, 使用高匿代理 (西刺免费代理IP)
								.build();//创建config
						get.setConfig(config);
						
						CloseableHttpResponse response = client.execute(get);
						
						Header[] headers = response.getAllHeaders();//获取所有响应头
						
						String result = EntityUtils.toString(response.getEntity(), "utf-8");
						System.out.println("66");
						response.close();
						client.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
					
				}
			});
//			service.shutdown();  
//	        while (true) {  
//	            if (service.isTerminated()) {  
//	                System.out.println("结束了！");  
//	                break;  
//	            }  
//	            try {
//					Thread.sleep(200);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}  
//	        }  
		}
		
	}
}
